package com.team.cloudsharing.controller.folder;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.team.cloudsharing.dto.Folder;
import com.team.cloudsharing.dto.FolderInformation;
import com.team.cloudsharing.dto.Message;
import com.team.cloudsharing.dto.PathDTO;
import com.team.cloudsharing.service.folder.FolderService;

@RestController
@RequestMapping("/folder")
public class FolderControllerImpl implements FolderController{

	final static Logger logger = Logger.getLogger(FolderControllerImpl.class);
	
	@Autowired
	FolderService folderService;
	
	@Override
	@RequestMapping(value = "/createFolder", method = RequestMethod.POST)
	public ResponseEntity<Message> createFolder(@RequestBody Folder folder) {
		logger.info("POST /folder/createFolder - folder: " + folder.toString());
		String statusCreateFolder = folderService.createFolder(folder);
		return new ResponseEntity<Message>(new Message(statusCreateFolder), HttpStatus.OK);
	}

	@Override
	@RequestMapping(value = "/moveFolderToTrash", method = RequestMethod.POST)
	public ResponseEntity<Message> moveFolderToTrash(@RequestBody Folder folder){
		logger.info("POST /folder/moveFolderToTrash - folder: " + folder.toString());
		String statusMoveFolderToTrash = folderService.moveFolderToTrash(folder);
		return new ResponseEntity<Message>(new Message(statusMoveFolderToTrash), HttpStatus.OK);
	}
	
	@Override
	@RequestMapping(value = "/restoreFolderFromTrash", method = RequestMethod.POST)
	public ResponseEntity<Message> restoreFolderFromTrash(@RequestBody Folder folder) {
		logger.info("POST /folder/restoreFolderFromTrash - folder: " + folder.toString());
		String statusRestoreFolderFromTrash = folderService.restoreFolderFromTrash(folder);
		return new ResponseEntity<Message>(new Message(statusRestoreFolderFromTrash), HttpStatus.OK);
	}
	
	@Override
	@RequestMapping(value = "/restoreFolderWithFolderParent", method = RequestMethod.POST)
	public ResponseEntity<Message> restoreFolderWithFolderParent(@RequestBody Folder folder) {
		logger.info("POST /folder/restoreFolderFromTrash - folder: " + folder.toString());
		String statusRestoreFolderFromTrash = folderService.restoreFolderWithFolderParent(folder, true);
		return new ResponseEntity<Message>(new Message(statusRestoreFolderFromTrash), HttpStatus.OK);
	}
	
	@Override
	@RequestMapping(value = "/deleteFolder", method = RequestMethod.POST)
	public ResponseEntity<Message> deleteFolder(@RequestBody Folder folder) {
		logger.info("POST /folder/deleteFolder - folder: " + folder.toString());
		String statusDeleteFolder = folderService.deleteFolder(folder);
		return new ResponseEntity<Message>(new Message(statusDeleteFolder), HttpStatus.OK);
	}

	@Override
	@RequestMapping(value = "/moveFolderToAnotherFolder", method = RequestMethod.POST)
	public ResponseEntity<Message> moveFolderToAnotherFolder(@RequestBody PathDTO pathDTO) {
		logger.info("POST /folder/moveFolderToAnotherFolder - moveFolder: " + pathDTO.toString());
		String statusMoveFolder = folderService.moveFolderToAnotherFolder(pathDTO);
		return new ResponseEntity<Message>(new Message(statusMoveFolder), HttpStatus.OK);
	}

	@Override
	@RequestMapping(value = "/renameFolder", method = RequestMethod.POST)
	public ResponseEntity<Message> renameFolder(@RequestBody PathDTO pathDTO) {
		logger.info("POST /folder/renameFolder - renameFolder: " + pathDTO.toString());
		String statusRenameFolder = folderService.renameFolder(pathDTO);
		return new ResponseEntity<Message>(new Message(statusRenameFolder), HttpStatus.OK);
	}

	@Override
	@RequestMapping(value = "/getFolderInformation", method = RequestMethod.GET)
	public ResponseEntity<FolderInformation> getFolderInformation(@RequestParam("userEmail") String userEmail, @RequestParam("folderPath") String folderPath) {
		FolderInformation folderInfo = folderService.getFolderInfomation(userEmail, folderPath);
		return new ResponseEntity<FolderInformation>(folderInfo, HttpStatus.OK);
	}

	@Override
	@RequestMapping(value="/getFolderUrl", method=RequestMethod.GET)
	public ResponseEntity<String> getFolderUrlByFolderId(@RequestParam("folderId")String folderId) {
		logger.info("POST /folder/getFolderUrl - folderId: " + folderId);
		String folderUrl = folderService.getFolderUrlByFolderId(folderId);
		return new ResponseEntity<String>(folderUrl, HttpStatus.OK);
	}

	@Override
	@RequestMapping(value = "/getFolderInformationById", method = RequestMethod.GET)
	public ResponseEntity<Folder> getFolderInformationById(@RequestParam("folderId") String folderId) {
		System.out.println();
		Folder folder = folderService.getFolderInformationById(Long.parseLong(folderId)); 
		return new ResponseEntity<Folder>(folder, HttpStatus.OK);
	}

	@RequestMapping(value = "/downloadFolder/{folderUrl}", method = RequestMethod.GET)
	public ResponseEntity<Folder> downloadFolder(@PathVariable("folderUrl") String folderUrl) {
		Folder folderDownload = folderService.downloadFolder(folderUrl);
		return new ResponseEntity<Folder>(folderDownload, HttpStatus.OK);
	}

}
